package com.zc58s.concurrent.spring.service;

import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component
public class JdbcService {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    private JdbcService jdbcService; // 自己注入自己

    @Transactional
    public void test() {
        jdbcTemplate.execute(String.format("INSERT INTO t1 (`username`) VALUES ('%s');", UUID.randomUUID().toString().toUpperCase()));
        //throw new RuntimeException("");
        System.out.println("test");
        jdbcService.doAny();
    }

    //
    // 事务怎么传播？
    // 解决事务失效的问题，自己注入自己
    @Transactional(propagation = Propagation.NEVER)
    public void doAny() {
        jdbcTemplate.execute(String.format("INSERT INTO t1 (`username`) VALUES ('%s');", UUID.randomUUID().toString().toUpperCase()));
        System.out.println("doAny");
    }
}
